﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using UDS.Components;

namespace UDS.Entity
{
    public class DocumentBriefInfo
    {
        #region properties
        public int DocID
        {
            get;
            set;
        }

        /// <summary>
        /// Get the title of this document.
        /// </summary>
        public virtual string Title
        {
            get;
            protected set;
        }

        public DateTime TimeCreated
        {
            get;
            set;
        }

        public DocumentStatus Status
        {
            get;
            set;
        }

        /// <summary>
        /// Get the display name of <see cref="Status"/>.
        /// </summary>
        public string StatusStr
        {
            get { return Utility.GetEnumDisplayName(Status); }
        }

        public int FlowID
        {
            get;
            set;
        }

        public string FlowName
        {
            get;
            private set;
        }

        public int StepID
        {
            get;
            set;
        }

        public string StepName
        {
            get;
            private set;
        }

        /// <summary>
        /// Get or set the ID of the creator.
        /// </summary>
        public int CreatorID
        {
            get;
            set;
        }

        /// <summary>
        /// Get or set the name of the creator.
        /// </summary>
        public string Creator
        {
            get;
            set;
        }

        /// <summary>
        /// Get or set the display name of the creator.
        /// </summary>
        public string CreatorRealName
        {
            get;
            set;
        }
        #endregion

        /// <summary>
        /// 我的申请
        /// </summary>
        public static IList<DocumentBriefInfo> GetMyDocument(string userName)
        {
            return GetDocumentList(userName, "dbo.sproc_Flow_GetMyDocument_V2");
        }

        /// <summary>
        /// 待批文档
        /// </summary>
        public static IList<DocumentBriefInfo> GetMyPostil(string userName)
        {
            return GetDocumentList(userName, "dbo.sproc_Flow_GetMyPostil_V2");
        }

        /// <summary>
        /// 已经批阅的文档
        /// </summary>
        public static IList<DocumentBriefInfo> GetPostiledDocument(string userName)
        {
            return GetDocumentList(userName, "dbo.sproc_Flow_GetPostiledDocument_V2");
        }

        private static IList<DocumentBriefInfo> GetDocumentList(string userName, string sproc)
        {
            var parameters = new[] {
                Database.MakeInParam("@StaffName", SqlDbType.NVarChar, 300, userName)
            };

            var db = new Database();
            var list = new List<DocumentBriefInfo>();
            using (var dr = db.RunDataReader(sproc, parameters))
            {
                while (dr.Read())
                {
                    list.Add(ReadDocumentBriefInfo(dr));
                }
            }
            return list;
        }

        private static DocumentBriefInfo ReadDocumentBriefInfo(IDataReader dr)
        {
            var b = new DocumentBriefInfo();
            ReadDocumentBriefInfo(dr, b);

            // Read the title of this document
            b.Title = Convert.ToString(dr["Title"]);
            return b;
        }

        protected static void ReadDocumentBriefInfo(IDataReader dr, DocumentBriefInfo briefInfo)
        {
            Debug.Assert(briefInfo != null);

            if (briefInfo.DocID == 0)
            {
                briefInfo.DocID = (int)dr["Doc_ID"];
            }
#if DEBUG
            else
            {
                int id = (int)dr["Doc_ID"];
                Debug.Assert(id == briefInfo.DocID);
            }
#endif

            briefInfo.TimeCreated = Utility.ReadSQLDate(dr["Doc_Added_Date"]);
            briefInfo.Status = (DocumentStatus)Utility.ReadInt32(dr["Doc_Status"], 0);
            briefInfo.FlowID = Utility.ReadInt32(dr["Flow_ID"], 0);
            briefInfo.FlowName = Convert.ToString(dr["Flow_Name"]);
            briefInfo.StepID = Utility.ReadInt32(dr["Step_ID"], 0);
            briefInfo.StepName = Convert.ToString(dr["Step_Name"]);
            briefInfo.CreatorID = (int)dr["Staff_ID"];
            briefInfo.Creator = Convert.ToString(dr["Staff_Name"]);
            briefInfo.CreatorRealName = Convert.ToString(dr["Realname"]);
        }
    }
}
